package hanjia;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/1/18-下午10:09
 * Since  :   1.0
 * Version:   1.0
 */
public class DynamicArray {
    private int nums[] = {};
    private int size = 0;
    private int capacity = 10;


    public void addLast(int element){
        add(size,element);
    }


    public void add(int index,int element){

        checkAndGrow();
        if(index>=0&&index<size){
            System.arraycopy(nums,index,nums,index+1,capacity-index);
        }
        nums[index]=element;
        size++;
    }

    public void checkAndGrow() {

        if (size==0) {
            nums=new int[capacity];
        }

        if(size==capacity){
            capacity+=capacity/2;
            int[] newArray = new int[capacity];
            System.arraycopy(nums,0,newArray,0,size);
            nums=newArray;
        }


    }




}
